package com.ruoyi.project.wx.service.impl;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.project.wx.dao.GoodsSpikeDao;
import com.ruoyi.project.wx.dao.GoodsSpikeTimeDao;
import com.ruoyi.project.wx.dao.MemberUserDao;
import com.ruoyi.project.wx.domain.GoodsSpikeTimeDO;
import com.ruoyi.project.wx.service.GoodsSpikeTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class GoodsSpikeTimeServiceImpl implements GoodsSpikeTimeService {
	@Autowired
	private GoodsSpikeTimeDao goodsSpikeTimeDao;
	@Autowired
	private GoodsSpikeDao goodsSpikeDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public GoodsSpikeTimeDO get(String id){
		return goodsSpikeTimeDao.get(id);
	}
	
	@Override
	public List<GoodsSpikeTimeDO> list(Map<String, Object> map){
		return goodsSpikeTimeDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsSpikeTimeDao.count(map);
	}
	
	@Override
	public int save(GoodsSpikeTimeDO goodsSpikeTime){
		return goodsSpikeTimeDao.save(goodsSpikeTime);
	}
	
	@Override
	public int update(GoodsSpikeTimeDO goodsSpikeTime){
		return goodsSpikeTimeDao.update(goodsSpikeTime);
	}
	
	@Override
	public int remove(String id){
		return goodsSpikeTimeDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsSpikeTimeDao.batchRemove(ids);
	}

	@Override
	public List<Map<String,Object>> getTime() {
		Map<String, Object> params = new HashMap<String,Object>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String today = format.format(date);
		params.put("today",today);
		List<GoodsSpikeTimeDO> timeList = goodsSpikeTimeDao.getTime(params);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		SimpleDateFormat shiFormat = new SimpleDateFormat("HH:mm");
		for (GoodsSpikeTimeDO timeDO : timeList) {
			Map<String,Object> map = new HashMap<String,Object>();
			Date startAt = timeDO.getStartAt();
			Date endAt = timeDO.getEndAt();
			boolean panStart = startAt.before(date);  //开始时间小于现在时间
			boolean panEnd = endAt.before(date);  //结束时间小于现在时间
			if (!panStart) {
				map.put("ans","即将开始");
				map.put("can","1");
			} else if (panStart && !panEnd) {
				map.put("ans","正在秒杀");
				map.put("can","0");
			} else if (panEnd) {
				map.put("ans","已经结束");
				map.put("can","-1");
			}
			map.put("startTime",shiFormat.format(startAt));
			map.put("endTime",shiFormat.format(endAt));
			map.put("timeId",timeDO.getId());
			list.add(map);
		}
		return list;
	}

	@Override
	public PageInfo<Map<String,Object>> getGoodsByTime(Map<String, Object> paramMap) {
		PageHelperUtils.page(paramMap);
		List<Map<String,Object>> list =goodsSpikeDao.getGoodsByTime(paramMap);
		/*if(paramMap.containsKey("userId")&&StringUtils.isNotBlank(paramMap.get("userId").toString())&&list.size()>0){
			MemberUserDO memberUserDO = memberUserDao.get(paramMap.get("userId").toString());
			for (Map<String, Object> map : list) {
				if(memberUserDO.getLevel()=='A'){
					map.put("newPrice", map.get("price_a").toString());
				}else if (memberUserDO.getLevel()=='B'){
					map.put("newPrice", map.get("price_b").toString());
				}else if (memberUserDO.getLevel()=='C'){
					map.put("newPrice", map.get("price_c").toString());
				}
			}
		}*/
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}


	
}
